package com.example.sfm.controller.admin;

import com.example.sfm.constant.MessageConstants;
import com.example.sfm.pojo.dto.FeedbackDto;
import com.example.sfm.pojo.dto.FeedbackQueryDto;
import com.example.sfm.pojo.dto.ReplyDto;
import com.example.sfm.result.Result;
import com.example.sfm.service.IFeedbackService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sfm
 * @since 2025-03-07
 */
@Tag(name = "管理员反馈管理", description = "管理员对反馈的管理接口")
@RestController("adminFeedbackController")
@RequestMapping("/admin/feedbacks")
@RequiredArgsConstructor
@Slf4j
public class FeedbackController {
    private final IFeedbackService feedbackService;

    /**
     * 创建反馈
     */
    @Operation(summary = "创建反馈", description = "根据提供的反馈信息创建一个新的反馈")
    @PostMapping("/create")
    public Result<?> createFeedback(@RequestBody @Validated @Parameter(description = "反馈信息") FeedbackDto dto) {
        log.info("创建反馈: {}", dto);
        feedbackService.createFeedback(dto);
        return Result.success(MessageConstants.ADD_OK);
    }

    /**
     * 分页查询反馈
     */
    @Operation(summary = "分页查询反馈", description = "根据查询条件分页查询反馈列表")
    @GetMapping("/list")
    public Result<?> listFeedbacks(@Validated @ParameterObject FeedbackQueryDto dto) {
        log.info("查询反馈列表: {}", dto);
        return Result.success(feedbackService.listFeedbacks(dto), MessageConstants.SELECT_OK);
    }

    /**
     * 更新反馈基本信息
     */
    @Operation(summary = "更新反馈信息", description = "根据反馈ID更新反馈信息")
    @PutMapping("/update/{id}")
    public Result<?> updateFeedback(@PathVariable @Parameter(description = "反馈ID",in = ParameterIn.PATH) Integer id, @RequestBody @Validated @Parameter(description = "更新内容") FeedbackDto dto) {
        log.info("更新反馈: ID={}, 内容={}", id, dto);
        feedbackService.updateFeedback(id, dto);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 处理反馈（添加回复）
     */
    @Operation(summary = "处理反馈", description = "根据反馈ID添加回复")
    @PutMapping("/reply/{id}")
    public Result<?> replyFeedback(@PathVariable @Parameter(description = "反馈ID",in = ParameterIn.PATH) Integer id, @RequestBody @Parameter(description = "回复内容") ReplyDto reply) {
        log.info("处理反馈: ID={}, 回复内容={}", id, reply);
        feedbackService.handleReply(id, reply);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }

    /**
     * 批量删除反馈
     */
    @Operation(summary = "批量删除反馈", description = "根据提供的反馈ID列表批量删除反馈")
    @DeleteMapping("/delete")
    public Result<?> deleteFeedbacks(@RequestParam @Parameter(description = "反馈ID列表",in = ParameterIn.QUERY) List<Integer> ids) {
        log.info("批量删除反馈: IDs={}", ids);
        feedbackService.deleteFeedbacks(ids);
        return Result.success(MessageConstants.OPERATION_SUCCESSFUL);
    }
}